import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Query,
  Delete,
  BadRequestException,
} from '@nestjs/common';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { UserEntity } from './entities/user.entity';

@Controller('user')
export class UserController { 
  constructor(private readonly userService: UserService) {}

  @Post()
  create(@Body() createUserDto: CreateUserDto) {
    return this.userService.create(createUserDto);
  }

  //获取用户列表
  @Get('lists')
  async findAll(
    @Query('pageNum') pageNum: number = 1,
    @Query('pageSize') pageSize: number = 10,
  ): Promise<{
    code: number;
    data: UserEntity[];
    total: number;
  }> {
    return this.userService.findAll(pageNum, pageSize);
  }

  //获取用户详情
  @Get('list') //http://localhost:3000/api/text/list?bid=1
  async findOne(
    @Query('uid') uid: number,
    @Query('pageNum') pageNum: number = 1,
    @Query('pageSize') pageSize: number = 10,
  ): Promise<{
    code: number;
    data: UserEntity[];
    total: number;
  }> {
    if (!uid) {
      throw new BadRequestException('缺少必填参数: uid');
    }
    return this.userService.findOne(uid, pageNum, pageSize);
  }

  // @Get(':id')
  // findOne(@Param('id') id: string) {
  //   return this.userService.findOne(+id);
  // }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
    return this.userService.update(+id, updateUserDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.userService.remove(+id);
  }
}
